module.exports = class ClockModel {

	constructor() {

	}

	 async now(pattern = "YYYY-MM-DD hh:mm:ss", nowDT = new Date(), timeZone = 8, types = ["YYYY", "MM", "DD", "hh", "mm",
		"ss"
	]) {
		// 默认东八区
		const curNowDT = this.getLocalTime(timeZone, nowDT);
		return types.reduce((pre, cur) => {
			return pre.replace(cur, this.captureDateTime(cur, curNowDT))
		}, pattern)
	}
	static now(pattern = "YYYY-MM-DD hh:mm:ss", nowDT = new Date(), timeZone = 8, types = ["YYYY", "MM", "DD", "hh",
		"mm",
		"ss"
	]) {
		// 默认东八区
		const curNowDT = this.getLocalTime(timeZone, nowDT);
		return types.reduce((pre, cur) => {
			return pre.replace(cur, this.captureDateTime(cur, curNowDT))
		}, pattern)
	}
	static getLocalTime(i = 8, d = new Date()) {
		//参数i为时区值数字，比如北京为东八区则输入+8,西5输入-5
		//参数d为要转换的时间(Date类型)
		var len = d.getTime();
		//本地时间与GMT时间的时间偏移差
		var offset = d.getTimezoneOffset() * 60000;
		//得到现在的格林尼治时间
		var utcTime = len + offset;
		return new Date(utcTime + 3600000 * i);
	}
    async getLocalTime(i, d) {
		//参数i为时区值数字，比如北京为东八区则输入+8,西5输入-5
		//参数d为要转换的时间(Date类型)
		var len = d.getTime();
		//本地时间与GMT时间的时间偏移差
		var offset = d.getTimezoneOffset() * 60000;
		//得到现在的格林尼治时间
		var utcTime = len + offset;
		return new Date(utcTime + 3600000 * i);
	}


	captureDateTime(tar, date) {
		const dtMap = new Map([
			["YYYY", date.getFullYear()],
			["MM", this.trim(date.getMonth() + 1)],
			["DD", this.trim(date.getDate())],
			["hh", this.trim(date.getHours())],
			["mm", this.trim(date.getMinutes())],
			["ss", this.trim(date.getSeconds())]
		])
		return dtMap.get(tar);
	}
	static captureDateTime(tar, date) {
		const dtMap = new Map([
			["YYYY", date.getFullYear()],
			["MM", this.trim(date.getMonth() + 1)],
			["DD", this.trim(date.getDate())],
			["hh", this.trim(date.getHours())],
			["mm", this.trim(date.getMinutes())],
			["ss", this.trim(date.getSeconds())]
		])
		return dtMap.get(tar);
	}
	 async trim(sour) {
		return sour < 10 ? `0${sour}` : sour
	}
	static trim(sour) {
		return sour < 10 ? `0${sour}` : sour
	}
	// 获取月的最后一天
	static getLastDayInMonth(year,month){
		return (year && month )? new Date(year,month,0) .getDate(): 0;
	}
	// 获取星期几 
	static getDayInWeek( InferredDate  , dt = this.getLocalTime()){
		const day = dt.getDay();
		return  InferredDate == undefined ? day : day == InferredDate;
	}


}
